<template>
    <div>
        <div>
            <el-config-provider :locale="zhCn">

                <!-- 工作日历 -->
                <el-row :gutter="24">
                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                            <el-form-item label="工作日历" label-position="top"
                                :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                                <div class="progress-bar">
                                    <div class="progress"></div>
                                </div>
                            </el-form-item>

                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="日期时间" label-position="top">
                                <el-date-picker v-model="formData.dateTime" style="width: 100%;" type="date"
                                    placeholder="" :size="size" value-format="YYYY-MM-DD" :disabled="!isEditing" />

                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="星期" label-position="top">
                                <el-input v-model="formData.week" placeholder="" style="/*width: 354px;*/"
                                    :disabled="!isEditing" />
                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="工作状态" label-position="top">
                                <el-input placeholder="" style="/*width: 354px;*/" :disabled="!isEditing">
                                    <template #prefix>
                                        <el-tag v-if="formData.workingCondition"
                                            :type="getTagType(formData.workingCondition)" style="margin-right: 5px;"
                                            effect="dark">
                                            {{ formData.workingCondition }}
                                        </el-tag>
                                    </template>
                                </el-input>
                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="详情" label-position="top">
                                <el-input v-model="formData.particulars" placeholder="" style="/*width: 354px;*/"
                                    :disabled="!isEditing" />
                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="节日" label-position="top">
                                <el-input v-model="formData.festival" placeholder="" style="/*width: 354px;*/"
                                    :disabled="!isEditing" />
                            </el-form-item>
                        </div>
                    </el-col>

                    <el-col :span="24">
                        <div class="grid-content ep-bg-purple-light"><el-form-item label="节假日加班补偿规定"
                                label-position="top">
                                <el-input v-model="formData.compensationProvisions" placeholder=""
                                    style="/*width: 354px;*/" :disabled="!isEditing" />
                            </el-form-item>
                        </div>
                    </el-col>

                    <!--  -->
                    <el-col :span="24">
                        <!-- 空白分隔区域 -->
                        <div style="height: 10px; border-top: 0px dashed #e4e7ed; margin: 6px 0;"></div>
                    </el-col>

                    <div class="bottom-submit-bar">
                        <el-button v-if="!isEditing" type="primary" @click="enableEditing">编辑</el-button>
                        <template v-else>
                            <el-button type="primary" @click="submitForm">提交</el-button>
                            <el-button @click="cancelEditing">取消</el-button>
                        </template>
                    </div>

                </el-row>
            </el-config-provider>
        </div>
    </div>
</template>

<script setup>
import { ref, defineEmits, watch } from 'vue';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import { ElMessage, ElMessageBox } from 'element-plus';
import request from '@/api/request';
import router from '@/router';

let emits = defineEmits(['ref-table'])
let props = defineProps(['data'])
let formData = ref({
    ...props.data
})

// 添加编辑状态控制变量
const isEditing = ref(false);
// 保存原始数据用于取消编辑时恢复
const originalData = ref({ ...props.data });

// 根据状态返回 Tag 类型
const getTagType = (status) => {
    switch (status) {
        case "工作日":
            return "success";
        case "公休日":
            return "primary";
        case "节假日":
            return "warning";
        default:
            return "default";
    }
};

// 启用编辑模式
const enableEditing = () => {
    isEditing.value = true;
};

// 提交表单
const submitForm = () => {
    console.log('提交数据:', formData.value);
    request({
        url: "/workingCalendar/update",
        method: "post",
        data: formData.value
    }).then(res => {
        // 提交后退出编辑模式
        isEditing.value = false;
        // 通知父组件刷新表格
        emits('ref-table');
        ElMessage.primary(res.msg);
    })
};

// 取消编辑
const cancelEditing = () => {
    // 恢复原始数据
    formData.value = { ...originalData.value };
    isEditing.value = false;
};
</script>

<style scoped>
/* 外层容器，可控制整体条的高度、圆角等 */
.progress-bar {
    height: 6px;
    background-color: #ccebf7;
    border-radius: 3px;
    display: flex;
    width: 100%;
    /* 确保进度条容器宽度占满父容器 */
    overflow: hidden;
    /* 防止子元素溢出 */
}

.progress {
    width: 8%;
    height: 10;
    background-color: #61c3ea;
    border-radius: 3px 0 0 3px;
    transition: width 0.3s ease;
    /* 添加宽度变化的过渡效果 */
}

.bottom-submit-bar {
    /* position: fixed; */
    /* 固定定位 */
    bottom: 0;
    /* 靠底部 */
    left: 0;
    width: 100%;
    background-color: #fff;
    /* 背景色，可根据需求调整 */
    border-top: 1px solid #eaeaea;
    /* 顶部边框，与页面区分 */
    padding: 12px 24px;
    /* 内边距，适配截图里的间距风格 */
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    /* 按钮靠左，可根据实际需求调整 */
    z-index: 10;
    /* 提高层级，避免被其他内容覆盖 */
}
</style>